using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace LieroClone
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Projectile test;
        Texture2D blank;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = Global.SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = Global.SCREEN_HEIGHT;
            IsFixedTimeStep = false;
            graphics.ApplyChanges();
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D bulletTex = Content.Load<Texture2D>("sprites\\bullet");
            Vector2 pos = new Vector2( 50.0f, 550.0f );
            Vector2 spd = new Vector2(125.0f, -125.0f );
            test = new Projectile(bulletTex, pos, Math.PI/4, 1, spd, true);
            test.alive = true;
            blank = Content.Load<Texture2D>("sprites\\blank");
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            double elapsedTime = gameTime.ElapsedGameTime.TotalSeconds;
            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Escape))
                this.Exit();

            if (ks.IsKeyDown(Keys.R))
            {
                test.pos = new Vector2(50.0f, 550.0f);
                test.vel = new Vector2(125.0f, -125.0f);
                test.alive = true;
            }

            test.update(elapsedTime);
            if (test.alive)
            {
                Console.WriteLine("Bullet velocity: X:" + test.vel.X + ". Y:" + test.vel.Y + ".");
                Console.WriteLine("Bullet world position: X:" + test.pos.X + ". Y:" + test.pos.Y + ".");
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.SaddleBrown);
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            //http://msdn.microsoft.com/en-us/library/bb313965(v=xnagamestudio.10).aspx
            drawDivider();
            test.draw(spriteBatch, new Vector2(200.0f, 250.0f), 1);
            test.draw(spriteBatch, new Vector2(200.0f, 250.0f), 2);
            test.pos += new Vector2(10.0f);
            test.draw(spriteBatch);
            test.pos -= new Vector2(10.0f);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void drawDivider()
        {
            spriteBatch.Draw(blank, new Vector2(400.0f, 0.0f), new Rectangle(0, 0, 1, 1),
                             Color.Black, 0, Vector2.Zero, new Vector2(1.0f, 500.0f), SpriteEffects.None, 0);
            spriteBatch.Draw(blank, new Vector2(0.0f, 500.0f), new Rectangle(0, 0, 1, 1),
                             Color.Black, 0, Vector2.Zero, new Vector2(800.0f, 1.0f), SpriteEffects.None, 0);
            
        }
    }
}
